package com.jule.teenpatti.game.play.impl;

import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.game.model.CardConstent;
import com.jule.teenpatti.game.model.PlayerInfo;
import com.jule.teenpatti.game.model.eenum.NextBetPlayerReasonEnum;
import com.jule.teenpatti.game.model.eenum.PlayerStateEnum;
import com.jule.teenpatti.game.play.AbstractTable;

public class BlindTable extends AbstractTable {

    public BlindTable(String gameId, String roomId, String tableId) {
        super(gameId, roomId, tableId);
    }

    @Override
    public int giveCardCounts() {
        return CardConstent.BLINDS_HAND_CARDS;
    }

    /**
     * 获得下一个行动的玩家
     *
     * @param preBetSeatNum
     * @return
     */
    @Override
    public PlayerInfo getNextBetPlayer(int preBetSeatNum, NextBetPlayerReasonEnum reasonEnum) {
        PlayerInfo nextBetPlayer = null;
        if (this.getInGamePlayers().size() <= GameConst.FINAL_PLAYER_NUM) {
            return null;
        }

        //计算下一个下注玩家的座位号
        int nextBetSeatNum = preBetSeatNum;
        while (true) {
            nextBetSeatNum -= 1;
            if (nextBetSeatNum < 1) {
                nextBetSeatNum = 5;
            }
            ////再次找到当前位置，说明已无合适下一个行动者，返回null
            if (nextBetSeatNum == preBetSeatNum) {
                return null;
            }
            //如果当前行动者的下家是庄家，则轮次加1
            if (nextBetSeatNum == firstBetSeatNum) {
                if(this.currRoundId == this.getRoomConfig().getMaxBlindRound()){
                    return null;
                }
                this.currRoundId += 1;
            }
            if (this.getInGamePlayers().containsKey(nextBetSeatNum)) {
                nextBetPlayer = this.getInGamePlayers().get(nextBetSeatNum);
                if (nextBetPlayer.getState() != PlayerStateEnum.fold && nextBetPlayer.getState() != PlayerStateEnum.showfail) {
                    //找到了合适的下一个行动者，结束循环
                    break;
                }
            }
        }
        //将下一个行动玩家，设置为牌桌当前行动玩家
        if (null != nextBetPlayer) {
            setCurrActionPlayerId(nextBetPlayer.getPlayerId());
            setCurrActionSeatNum(nextBetPlayer.getSeatNum());
        }
        return nextBetPlayer;
    }
}
